#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>

/************************************************************************
Copyright (c) tiny
file            :beep.c
author          :tiy
version         :1.0.0
description     :beep driver file
other           :none
log             :first version 2023-03-05
************************************************************************/

/*
    beep gpio pin:GPIO1_IO19(pad:UART1_RTS_B)
*/

#define     BEEP_CNT        1
#define     BEEP_NAME       "beep"
#define     BEEP_ON         1
#define     BEEP_OFF        0

struct beep_dev{
    dev_t devid;
    struct cdev cdev;
    struct class *class;
    struct device *device;
    int major;
    int minor;
    struct device_node *nd;
    int beep_gpio;
};

struct beep_dev beepdev;

static int beep_open(struct inode *inode, struct file *filp)
{
    filp->private_data = &beepdev;
    return 0;
}

static ssize_t beep_read(struct file *filp, char __user *buf, 
                        size_t cnt, loff_t *offt)
{
    return 0;
}

static ssize_t beep_write(struct file *filp, const char __user *buf,
                        size_t cnt, loff_t *offt)
{
    int retvalue = 0;
    unsigned char databuf[1];
    unsigned char beepstat;
    struct beep_dev *dev = filp->private_data;

    retvalue = copy_from_user(databuf, buf ,cnt);
    if(retvalue < 0) {
        printk("kernel write failed!\n");
        return -EFAULT;
    }

    beepstat = databuf[0];

    if(beepstat == BEEP_ON) {
        gpio_set_value(dev->beep_gpio, 1);
    } else if (beepstat == BEEP_OFF) {
        gpio_set_value(dev->beep_gpio, 0);
    }

    return 0;
}

static int beep_release(struct inode *inode, struct file *filp)
{
    return 0;
}

static struct file_operations beep_fops = {
    .owner = THIS_MODULE,
    .open = beep_open,
    .read = beep_read,
    .write = beep_write,
    .release = beep_release,
};

static int __init beep_init(void)
{
    int ret = 0;
    /* 从设备数获取beep引脚配置 */
    /* 1.获取设备节点 */
    beepdev.nd = of_find_node_by_path("/beep");
    if(beepdev.nd == NULL) {
        printk("beep node no found!\n");
        return -EINVAL;
    } else {
        printk("beep node found.\n");
    }

    /* 2.获取GPIO num */
    beepdev.beep_gpio = of_get_named_gpio(beepdev.nd, "beep-gpio", 0);
    if(beepdev.beep_gpio <  0) {
        printk("can't not get beep-gpio.\n");
        return -EINVAL;
    }
    printk("beep-gpio num = %d.\n", beepdev.beep_gpio);
    
    /* 3.设置gpio 为输出模式，默认值得为1 */
    ret = gpio_direction_output(beepdev.beep_gpio, 1);
    if(ret) {
        printk("can't set gpio!\n");
    }

    /* 注册字符设备驱动 */
    /* 1.创建设备号 */
    if(beepdev.major) {
        beepdev.devid = MKDEV(beepdev.major, 0);
        ret = register_chrdev_region(beepdev.devid, BEEP_CNT, BEEP_NAME);
        if(ret) {
            printk("Error get major num %d for driver.\n", beepdev.major);
            return -EINVAL;
        }
    } else {
        ret = alloc_chrdev_region(&beepdev.devid, 0, BEEP_CNT, BEEP_NAME);
        if(ret){
            printk("Unable to allocate chrdev region\n");
            return -EINVAL;
        }
        beepdev.major = MAJOR(beepdev.devid);
        beepdev.minor = MINOR(beepdev.devid);
    }
    printk("beep major=%d, minor=%d.\n", beepdev.major, beepdev.minor);
    
    /* 2.chrdev 初始化 */
    beepdev.cdev.owner = THIS_MODULE;
    cdev_init(&beepdev.cdev, &beep_fops);

    /* 3.chrdev注册 */
    cdev_add(&beepdev.cdev, beepdev.devid, BEEP_CNT);

    /* 4.创建类 */
    beepdev.class = class_create(THIS_MODULE, BEEP_NAME);
    if(IS_ERR(beepdev.class)) {
        return PTR_ERR(beepdev.class);
    }

    /* 5.创建设备 */
    beepdev.device = device_create(beepdev.class, NULL, beepdev.devid, NULL, BEEP_NAME);
    if(IS_ERR(beepdev.device)){
        return PTR_ERR(beepdev.device);
    }

    return 0;
}

static void __exit beep_exit(void)
{
    /* 注销字符设备驱动 */
    cdev_del(&beepdev.cdev);
    unregister_chrdev_region(beepdev.devid, BEEP_CNT);

    device_destroy(beepdev.class, beepdev.devid);
    class_destroy(beepdev.class);
}

module_init(beep_init);
module_exit(beep_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Tiny");